package com.dat255.tentatime;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

import com.dat255.internalstorage.FavoritesManagement;

public class AddExamActivity extends Activity{

	private EditText courseid, coursename, courseplace;
	private DatePicker datepicker;
	private TimePicker timepicker;
	private Button buttonSave, buttonCancel;
	private FavoritesManagement fav = new FavoritesManagement(this,15);
	
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.exam_form);
		
		 //Assign components
        courseid = (EditText) findViewById(R.id.formCourseID);
        coursename = (EditText) findViewById(R.id.formCourseName);
        courseplace = (EditText) findViewById(R.id.formPlace);
        
        datepicker = (DatePicker) findViewById(R.id.datePicker);
        timepicker = (TimePicker) findViewById(R.id.timePicker);
        
        buttonSave = (Button) findViewById(R.id.buttonSave);
        buttonCancel = (Button) findViewById(R.id.buttonCancel);
        
        buttonSave.setOnClickListener(saveListener);
        buttonCancel.setOnClickListener(cancelListener);
        
 
        
	}


	//Create an anonymous implementation of OnClickListener
    private OnClickListener saveListener = new OnClickListener() {
        public void onClick(View v) {
        	
        	
        	
        	String time = timepicker.getCurrentHour().toString() + ":" + timepicker.getCurrentMinute();
        	// 0 = jan
        	String date = datepicker.getDayOfMonth() + "-" + (datepicker.getMonth()+1) + "-" + datepicker.getYear();
        	//TODO: validate the input
        	
        	Exam newExam = new Exam(courseid.getText().toString(), coursename.getText().toString(),time, date, courseplace.getText().toString());
        
        	if(fav.saveFavorite(newExam)){
        		
        		Intent examIntent = new Intent(AddExamActivity.this, FavouriteActivity.class);
    			AddExamActivity.this.startActivity(examIntent);	
    			finish();
        		
        	};
        	
        	
        	
        	//TODO: store the information on the server
        	//TODO: Show message to the admin whether it was successful or not
        	
                       
      
        }
    };
     
    // Create an anonymous implementation of OnClickListener
    private OnClickListener cancelListener = new OnClickListener() {
        public void onClick(View v) {
      
        	finish();
        	
        	
    
        } 
    };
	
	
	

}
